package com.comum.javahomework420242025.controller;

import com.comum.javahomework420242025.pojo.*;
import com.comum.javahomework420242025.repository.ChanceRepository;
import com.comum.javahomework420242025.repository.ClientRepository;
import com.comum.javahomework420242025.repository.KeyManRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@RestController
public class KeyManController {
    @Autowired
    private KeyManRepository keyManRepository;
    @Autowired
    private ChanceRepository chanceRepository;
    @PostMapping("/addKeyMan")
    public Result addKeyMan(String clientcompanyid, @RequestBody KeyMan keyMan){
        System.out.println(clientcompanyid);
        System.out.println(keyMan);
        keyManRepository.save(keyMan);
        return Result.success();
    }
    @PutMapping("/updateKeyMan")
    public Result updateKeyMan(@RequestBody KeyMan keyMan){
        System.out.println(keyMan);
        keyManRepository.save(keyMan);
        return Result.success();
    }
    @GetMapping ("/getKeyMan")
    public Result<List<KeyMan>> getUser(String str, String clientcompanyid){
        System.out.println(str);
        System.out.println(clientcompanyid);
        List<KeyMan> pb = keyManRepository.selectByStr(str,clientcompanyid);
        return Result.success(pb);
    }
    @DeleteMapping("/deleteKeyMan")
    public Result deleteClient(String id){
        System.out.println(id);
        List<Chance> res = chanceRepository.findByKeyManId(id);
        System.out.println(res);
        if(res.size()!=0){
            return Result.error("该关键人物存在关联信息，无法删除！");
        }
        keyManRepository.deleteById(Long.valueOf(id));
        return Result.success();
    }
    @GetMapping("/getKeyManByID")
    public Result<KeyMan> getKeyManByID(Long id){
        Optional<KeyMan> keyMan = keyManRepository.findById(id);
        System.out.println("keyman是" + keyMan.get());
        return Result.success(keyMan.get());
    }
}
